module.exports = class student_mod extends require("./model") {
  // 获取学生一定数量通知
  static getNoticeMod(user_classes, pageSize, currPage) {
    pageSize -= 0;
    currPage -= 0;
    let startIndex = (currPage - 1) * pageSize;
    return new Promise((resolve, reject) => {
      const sql =
        "select * from `notice` where class like '%" +
        user_classes +
        "%' order by create_time desc limit ?,? ";
      this.query(sql, [startIndex, pageSize])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 获取老师一定数量通知
  static getTeacherNoticeMod(user_classes, pageSize, currPage) {
    pageSize -= 0;
    currPage -= 0;
    let startIndex = (currPage - 1) * pageSize;
    return new Promise((resolve, reject) => {
      let sql = "select * from `notice` where class like ";
      user_classes.forEach((item, index) => {
        if (index == 0) {
          sql += `"%${item}%"`;
        } else {
          sql += ` or class like "%${item}%"`;
        }
      });
      sql += " order by create_time desc limit ?,? ";
      this.query(sql, [startIndex, pageSize])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 获取老师通知总数
  static getTeacherNoticeTotal(user_classes) {
    return new Promise((resolve, reject) => {
      let sql = "select count(1) count from `notice` where class like ";
      user_classes.forEach((item, index) => {
        if (index == 0) {
          sql += `"%${item}%"`;
        } else {
          sql += ` or class like "%${item}%"`;
        }
      });
      this.query(sql)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
  // 获取学生通知总数
  static getNoticeTotal(user_classes) {
    return new Promise((resolve, reject) => {
      let sql =
        "select count(1) count from `notice` where class like '%" +
        user_classes +
        "%'";
      this.query(sql)
        .then((res) => resolve(res))
        .catch((err) => reject(err));
    });
  }

  //获取已读通知
  static getNoticeReadMod(user_id) {
    return new Promise((resolve, reject) => {
      let sql = "select * from `read` where u_id=" + user_id;
      this.query(sql)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(reject);
        });
    });
  }

  // 已读转未读
  static goUnReadMod(u_id, n_id) {
    u_id -= 0;
    n_id -= 0;
    return new Promise((resolve, reject) => {
      let sql = "delete from `read` where u_id=? and n_id=?";
      this.query(sql, [u_id, n_id])
        .then((res) => {
          resolve("已读转未读成功~~~");
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 未读转已读
  static goReadedMod(u_id, n_id) {
    u_id -= 0;
    n_id -= 0;
    return new Promise((resolve, reject) => {
      let sql = "insert into `read`(u_id,n_id) values(?,?)";
      this.query(sql, [u_id, n_id])
        .then((res) => {
          resolve("未读转已读成功~~~");
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  /* -------------------健康填报表------------------*/

  // 提交健康申报表
  static setHeathMod(
    u_id,
    temperature,
    godanger,
    fever,
    leaveout,
    hesuan,
    mask,
    kills
  ) {
    return new Promise((resolve, reject) => {
      const sql = `insert into health(u_id, temperature, godanger, fever, leaveout, hesuan, mask, kills) values(${u_id},"${temperature}","${godanger}","${fever}","${leaveout}","${hesuan}","${mask}","${kills}")`;
      this.query(sql)
        .then((res) => {
          resolve("提交健康申报表成功~~~");
        })
        .catch((err) => {
          reject("提交健康申报表失败~~~");
        });
    });
  }

  // 获取当天的填报表
  static getHealthNowDayPageMod(newDate, lastDate, currPage, pageSize) {
    pageSize -= 0;
    let startIndex = (currPage - 1) * pageSize;
    return new Promise((resolve, reject) => {
      let sql =
        "select * from health where create_time between ? and ? limit ?,?";
      this.query(sql, [newDate, lastDate, startIndex, pageSize])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 获取当天填报表的总数量
  static getHealthNowDayPageTotal(newDate, lastDate) {
    return new Promise((resolve, reject) => {
      let sql =
        "select count(1) count from health where create_time between ? and ? ";
      this.query(sql, [newDate, lastDate])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 获取当天某用户报表
  static getHealthNowDayByIdMod(u_id, startTime, endTime) {
    return new Promise((resolve, reject) => {
      let sql =
        "select * from health where create_time > ? and create_time < ? and u_id = ?";
      this.query(sql, [startTime, endTime, u_id])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 获取当天所有报表
  static getHealthNowDayMod(newDate, lastDate) {
    return new Promise((resolve, reject) => {
      let sql = "select * from health where create_time between ? and ?";
      console.log(newDate, lastDate);
      this.query(sql, [newDate, lastDate])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 获取当月所有报表
  static getHealthNowMonthMod(nowMonth, lastMonth) {
    return new Promise((resolve, reject) => {
      let sql =
        "select * from health where create_time > ? and create_time < ?";
      this.query(sql, [nowMonth, lastMonth])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 获取所有报表
  static getAllHealthMod() {
    return new Promise((resolve, reject) => {
      let sql = "select * from health";
      this.query(sql)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  //  学生请假表申请
  static setLeaveMod(u_id, classes, reason, leavetype, start_time, end_time) {
    return new Promise((resolve, reject) => {
      let sql =
        "insert into `leave`(u_id, classes, reason, leavetype, start_time, end_time) values(?,?,?,?,?,?)";
      this.query(sql, [u_id, classes, reason, leavetype, start_time, end_time])
        .then((res) => {
          resolve("提交成功");
        })
        .catch((err) => {
          reject("提交失败");
        });
    });
  }
};
